package com.gjb6.customer.account;

import android.text.TextUtils;

import com.gjb6.customer.app.MyApplication;
import com.google.gson.annotations.SerializedName;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

/**
 * 账号类
 * <p>
 * 数据直接写到sp里，从内存中读取
 * <p>
 * user_id : test
 * real_name : 真实姓名
 * sign : 签名
 * portrait :
 * phone : 18088888888
 * gender : WOMAN
 * company : 爬虫
 * department : IT事业部
 * position : null
 * ext_info :
 * dateline : 1468217954
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016/7/12 16:35
 * @Version: 1.0.0
 */
public class Account {

    private static final String SP_FILENAME_ACCOUNT = Account.class.getSimpleName();
    private static final String KEY_USER_ID = "user_id";
    private static final String KEY_LOGIN_NAME = "login_name";
    private static final String KEY_PHONE = "phone";
    private static final String KEY_AVATAR = "avatar";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_CUSTOMER_NAME= "customerName";
    private static final String KEY_CUSTOMER_PORTRAIT= "portrait";
    private static final String KEY_IS_NULL = "is_null";
    private static final String KEY_LATITUDE = "latitude";
    private static final String KEY_LONGITUDE = "longitude";
    /**
     * user_id : 403
     * login_name : 13215018061
     * nick_name : 13215018061
     * phone : 13215018061
     * gender : 0
     * avatar : http://oo1e5sko2.bkt.clouddn.com/crawler.jpg
     * locked : false
     * create_time : 1498128926000
     * update_time : 1498128926000
     * province : null
     * city : null
     * district : null
     * birthday : null
     * is_use : 0
     * last_login_time : 1499769136000
     * del : false
     */
    @SerializedName("user_id")
    private int mUserId;
    @SerializedName("phone")
    private String mPhone;
    @SerializedName("customerName")
    private String mCustomerName;
    @SerializedName("portrait")
    private String mCustomerPortrait;
    @SerializedName("is_null")
    private boolean mIs_null;
    @SerializedName("latitude")
    private String mLatitude;
    @SerializedName("longitude")
    private String mLongitude;

    private String login_name;
    private String nick_name;
    private int gender;
    private String avatar;
    private boolean locked;
    private long create_time;
    private long update_time;
    private Object province;
    private Object city;
    private Object district;
    private Object birthday;
    private int is_use;
    private long last_login_time;
    private boolean del;
    private String mPassWord;

    public Account() {
    }

    /**
     * 初始化，从SP中读取
     */
    public static Account createFromSp() {
        Account account = new Account();
        account.setUserId(getInt(KEY_USER_ID));
        account.setPassWord(getString(KEY_PASSWORD));
        account.setPhone(getString(KEY_PHONE));
        return account;
    }
    /**
     * 初始化, 克隆一个对象
     */
    public static Account createFrom(Account acct) {
        Account account = new Account();
        account.setUserId(acct.getUserId());
        account.setPassWord(acct.getPassWord());
        account.setPhone(acct.getPhone());
        return account;
    }

    public String getLongitude (){
        return mLongitude;
    }
    public void setLongitude(String longitude) {
        if (!TextUtils.equals(this.mLongitude, longitude)) {
            mLongitude = longitude;
            saveString(KEY_LONGITUDE,longitude);
        }
    }


    public String getLatitude() {
        return mLatitude;
    }

    public void setLatitude(String latitude) {
        if (!TextUtils.equals(this.mLatitude, latitude)) {
            mLatitude = latitude;
            saveString(KEY_LATITUDE, mLatitude);
        }
    }









    public boolean getIs_null() {
        return mIs_null;
    }

    public void setIs_null(boolean is_null) {
        if (!(mIs_null == is_null)) {
            mIs_null = is_null;
            saveBoolean(KEY_IS_NULL, mIs_null);
        }
    }

    public String getPassWord() {
        return mPassWord;
    }

    public void setPassWord(String passWord) {
        if (!TextUtils.equals(this.mPassWord, passWord)) {
            mPassWord = passWord;
            saveString(KEY_PASSWORD, mPassWord);
        }
    }


    private static void saveInt(String key, int value) {
        SharePreferenceUtil.saveInt(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static int getInt(String key) {
        return SharePreferenceUtil.getInt(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }
    private static void saveString(String key, String value) {
        SharePreferenceUtil.saveString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static String getString(String key) {
        return SharePreferenceUtil.getString(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key);
    }

    private static void saveBoolean(String key, Boolean value) {
        SharePreferenceUtil.saveBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, value);
    }

    private static Boolean getBoolean(String key) {
        return SharePreferenceUtil.getBoolean(MyApplication.getAppContext(), SP_FILENAME_ACCOUNT, key, false);
    }


    public int getUserId() {
        return mUserId;
    }

    public void setUserId(int userId) {
        if (!(mUserId == userId)) {
            mUserId = userId;
            saveInt(KEY_USER_ID, mUserId);
        }
    }

    public String getPhone() {
        return mPhone;
    }

    public void setPhone(String phone) {
        if (!TextUtils.equals(this.mPhone, phone)) {
            mPhone = phone;
            saveString(KEY_PHONE, mPhone);
        }
    }

    public String getCustomerPortrait() {
        return mCustomerPortrait;
    }

    public void setCustomerPortrait(String customerPortrait) {
        if (!TextUtils.equals(this.mCustomerPortrait, customerPortrait)) {
            mCustomerPortrait = customerPortrait;
            saveString(KEY_CUSTOMER_PORTRAIT, mCustomerPortrait);
        }
    }



    public String getCustomerName() {
        return mCustomerName;
    }

    public void setCustomerName(String customerName) {
        if (!TextUtils.equals(this.mCustomerName, customerName)) {
            mCustomerName = customerName;
            saveString(KEY_CUSTOMER_NAME, mCustomerName);
        }
    }


    public void setLogin_name(String login_name) {
        this.login_name = login_name;
    }

    public void setNick_name(String nick_name) {
        this.nick_name = nick_name;
    }


    public void setGender(int gender) {
        this.gender = gender;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public void setCreate_time(long create_time) {
        this.create_time = create_time;
    }

    public void setUpdate_time(long update_time) {
        this.update_time = update_time;
    }

    public void setProvince(Object province) {
        this.province = province;
    }

    public void setCity(Object city) {
        this.city = city;
    }

    public void setDistrict(Object district) {
        this.district = district;
    }

    public void setBirthday(Object birthday) {
        this.birthday = birthday;
    }

    public void setIs_use(int is_use) {
        this.is_use = is_use;
    }

    public void setLast_login_time(long last_login_time) {
        this.last_login_time = last_login_time;
    }

    public void setDel(boolean del) {
        this.del = del;
    }



    public String getLogin_name() {
        return login_name;
    }

    public String getNick_name() {
        return nick_name;
    }


    public int getGender() {
        return gender;
    }

    public String getAvatar() {
        return avatar;
    }

    public boolean getLocked() {
        return locked;
    }

    public long getCreate_time() {
        return create_time;
    }

    public long getUpdate_time() {
        return update_time;
    }

    public Object getProvince() {
        return province;
    }

    public Object getCity() {
        return city;
    }

    public Object getDistrict() {
        return district;
    }

    public Object getBirthday() {
        return birthday;
    }

    public int getIs_use() {
        return is_use;
    }

    public long getLast_login_time() {
        return last_login_time;
    }

    public boolean getDel() {
        return del;
    }

    @Override
    public String toString() {
        return "Account{" +
                "mUserId=" + mUserId +
                ", mPhone='" + mPhone + '\'' +
                ", login_name='" + login_name + '\'' +
                ", nick_name='" + nick_name + '\'' +
                ", gender=" + gender +
                ", avatar='" + avatar + '\'' +
                ", locked=" + locked +
                ", create_time=" + create_time +
                ", update_time=" + update_time +
                ", province=" + province +
                ", city=" + city +
                ", district=" + district +
                ", birthday=" + birthday +
                ", is_use=" + is_use +
                ", last_login_time=" + last_login_time +
                ", del=" + del +
                ", mPassWord='" + mPassWord + '\'' +
                '}';
    }
}
